﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Reporting.WebForms;
using System.Data;
using Runda.CMS.Bll;
using Runda.CMS.Common.Entity;
using Runda.CMS.Common.Utility;
using System.Text;

namespace Runda.CMS.Portal.Report
{
    public partial class SalesRpt_102 : BasePage
    {
        public string SortString
        {
            get
            {
                if (ViewState["SortString"] == null)
                {
                    ViewState["SortString"] = "customername";
                }

                return ViewState["SortString"].ToString();
            }
            set { ViewState["SortString"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                for (int i = 2012; i <= 2015; i++)
                {
                    ddlBudgetYear.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }
                DataTable dt = new BudgetBll().Companylist(base.CurrentUserID.ToString());
                ddlCompany.DataSource = dt;
                ddlCompany.DataTextField = "Departmentname";
                ddlCompany.DataValueField = "DepartmentCode";
                ddlCompany.DataBind();
                ddlCompany.Items.Add(new ListItem("--不限--", "0"));
                ddlCompany.SelectedValue = "0";

                
                BindGrid();
           }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            BindGrid();
        }

        protected void gridAccessoryList_ItemDataBound(object sender, Telerik.WebControls.GridItemEventArgs e)
        {

        }

        private void BindGrid()
        {
            string sb = getWhereClause();

            //DataTable dt = new BillBll().SaleBillsStat(sb, SortString);


            //gridAccessoryList.DataSource = dt;
            //gridAccessoryList.DataBind();
        }

        protected void gridAccessoryList_SortCommand(object source, Telerik.WebControls.GridSortCommandEventArgs e)
        {
            string OrderBy = e.SortExpression + " " + (e.NewSortOrder == Telerik.WebControls.GridSortOrder.Ascending ? "asc" : "desc");
            SortString = OrderBy;
            BindGrid();
            

        }

        private string getWhereClause()
        {
            StringBuilder sb = new StringBuilder();

            //if (base.CurrentUserEntity.UserName == "郭海涛"
            //    || base.CurrentUserEntity.UserName == "史莹"
            //    || base.CurrentUserEntity.UserName == "王静"
            //    || base.CurrentUserEntity.UserName == "顾悦"
            //    || base.CurrentUserEntity.UserName == "宋欣"
            //    || base.CurrentUserEntity.UserName == "徐雪峰"
            //    || base.CurrentUserEntity.UserName == "胡日成"
            //    || base.CurrentUserEntity.UserName == "崔丽丽"
            //    || base.CurrentUserEntity.UserName == "官俊"
            //    || base.CurrentUserEntity.UserName == "李娜"
            //    || base.CurrentUserEntity.UserName == "张佳锦"
            //    )
            //{
            //    sb.Append(" and (a.[company] in(").Append("select DepartmentCode from cms_sys_Department where iscompany = 1 and DepartmentID in(select DepartmentID from Cms_Sys_UserDepartment_DataRight where userid ='").Append(base.CurrentUserID.ToString()).Append("')))");
            //}
            //else
            //{
            //    sb.Append(" and (a.[sales] in(").Append("select username from vw_sysUser where DepartmentID in(select DepartmentID from Cms_Sys_UserDepartment_DataRight where userid ='").Append(base.CurrentUserID.ToString()).Append("'))");
            //    sb.Append(" or a.[sales] ='").Append(base.CurrentUserEntity.UserName).Append("'");
            //    sb.Append(" or a.[Producter] ='").Append(base.CurrentUserEntity.UserName).Append("'");
            //    sb.Append(" or a.[ProductManager] ='").Append(base.CurrentUserEntity.UserName).Append("')");
            //}

            if (ddlBudgetYear.SelectedValue != string.Empty)
            {
                sb.Append(" and datepart(year,[dDate])  = ").Append(ddlBudgetYear.SelectedValue);
            }

            if (ddlStatPath2.SelectedValue == "1")
            { sb.Append(" and cCusCode in('rd','rdsy','kxws','qdyx','hcsw','hz')"); }
            else if (ddlStatPath2.SelectedValue == "2")
            { sb.Append(" and cCusCode not in('rd','rdsy','kxws','qdyx','hcsw','hz')"); }

            if (ddlCompany.SelectedValue != "0" && ddlStatPath1.SelectedValue=="1")
            {
                sb.Append(" and BillUnit='").Append(ddlCompany.SelectedValue.ToString()).Append("'");
            }

            if (ddlCompany.SelectedValue != "0" && ddlStatPath1.SelectedValue == "2")
            {
                sb.Append(" and BudgetUnit='").Append(ddlCompany.SelectedValue.ToString()).Append("'");
            }

            //if (this.ddlDepartment.SelectedValue != "0")
            //{
            //    sb.Append(" and a.sales in(select UserName from vw_sysUser where DepartmentName='").Append(ddlDepartment.SelectedValue.ToString()).Append("')");
            //}

            //if (ddlSales.SelectedValue != "0")
            //{
            //    sb.Append(" and a.sales='").Append(ddlSales.SelectedValue.ToString()).Append("'");
            //}

            //if (this.ddlProductCategory.SelectedValue != "0")
            //{
            //    sb.Append(" and a.productCategoryCode in(select CategoryCode from Cms_Ctrt_InstrumentCategory where FactoryName='").Append(ddlProductCategory.SelectedValue.ToString()).Append("')");
            //}

            if (txtCustomerName.Text.Trim() != "")
            {
                sb.Append(" and CustomerName like '%").Append(txtCustomerName.Text.Trim()).Append("%'");
            }

            return sb.ToString();
        }

       
        
        protected void btnExportExcel_Click(object sender, EventArgs e)
        {
            string sb = getWhereClause();
            BillBll bll = new BillBll();

            DataTable dt = bll.ExportSaleBillsStat(sb);

            Export.Export2Excel(dt, "销售明细统计", Page.Response);

        }
    }
}
